import Vue from 'vue'
import loading from '../plugins/loading';
//这里其实是引入vuex的
import store from '../store/index';
import {Message} from 'element-ui';
import axios from 'axios';
import httpStatus from '../plugins/httpStatus';

window.axios = axios;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
const __axios = axios.create({
    baseURL: '/',
    timeout: 500000
});

const axiosUnLoad = axios.create({
    baseURL: '/',
    timeout: 500000
});
//将axios附加在原型链上
Object.defineProperties(Vue.prototype, {
    axios: {
        get() {
            return __axios
        }
    },
    uaxios: {
        get() {
            return axiosUnLoad
        }
    }
})

//axios请求与响应 的钩子
__axios.interceptors.request.use(function (config) {
    loading.open()
    return config;
}, function (error) {
    return Promise.reject(error);
});

// Add a response interceptor
__axios.interceptors.response.use(function (response) {
    loading.close()
    if (response.data.message) {
        Message({
            message: response.data.message,
            type: 'success'
        })
    }
    //直接返回data数据
    return response.data;
}, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    loading.close()
    //axios 自带的响应错误的集合

    if (error && error.response) {
        let status = error.response.status
        let data = error.response.data
        switch (status) {
            case 422:
                //这里配合vuex掉用密码的方法,使用axios可以接受后台异步返回的errors信息
                store.commit('setErrors', data.errors)
                break;
            default:
                let message = data.message ? data.message : httpStatus(status)
                Message({
                    message: message,
                    type: 'error'
                })
        }
    }


    return Promise.reject(error);
});
//没有load的axios
axiosUnLoad.interceptors.request.use(function (config) {

    return config;
}, function (error) {
    return Promise.reject(error);
});

// Add a response interceptor
axiosUnLoad.interceptors.response.use(function (response) {

    if (response.data.message) {
        Message({
            message: response.data.message,
            type: 'success'
        })
    }
    //直接返回data数据
    return response.data;
}, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    loading.close()
    //axios 自带的响应错误的集合

    if (error && error.response) {
        let status = error.response.status
        let data = error.response.data
        switch (status) {
            case 422:
                //这里配合vuex掉用密码的方法,使用axios可以接受后台异步返回的errors信息
                store.commit('setErrors', data.errors)
                break;
            default:
                let message = data.message ? data.message : httpStatus(status)
                Message({
                    message: message,
                    type: 'error'
                })
        }
    }


    return Promise.reject(error);
});
